package br.inf.solutions.apresentacoes.ssp.control.bulletin;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import br.com.vexillum.util.Return;
import br.com.vexillum.util.SpringFactory;
import br.inf.solutions.apresentacoes.ssp.control.BaseController;
import br.inf.solutions.apresentacoes.ssp.control.PublicationController;
import br.inf.solutions.apresentacoes.ssp.model.Publication;
import br.inf.solutions.apresentacoes.ssp.model.bulletin.Bulletin;
import br.inf.solutions.apresentacoes.ssp.model.bulletin.BulletinPublishedItem;
import br.inf.solutions.apresentacoes.ssp.model.bulletin.BulletinTopics;
import br.inf.solutions.apresentacoes.ssp.model.bulletin.BulletinType;

@Service
@Scope("prototype")
public class BulletinPublishedItensController extends BaseController<BulletinPublishedItem> {

	public BulletinPublishedItensController() {
		super(BulletinPublishedItem.class);
	}
	
	protected Publication getPublishedPublication(){
		PublicationController controller = SpringFactory.getController("publicationController", PublicationController.class, null);
		return controller.getPublicatedPuplication();
	}

	public Return searchPublicatedItens(){
		Bulletin bulletin = (Bulletin) getData().get("bulletin");
		
		BulletinType type = bulletin.getBulletinType();
		Long numberBulletin =bulletin.getNumberBulletin();
		Integer yearBulletin = bulletin.getYearBulletin();
		Integer part = getData().get("bulletinPart") == null ? null : (Integer) getData().get("bulletinPart");
		BulletinTopics topic = getData().get("bulletinTopic") == null ? null : (BulletinTopics) getData().get("bulletinTopic");
		
		String sql = "FROM BulletinPublishedItem WHERE item.publication = '" + getPublishedPublication().getId() + "' AND "
					 + "bulletin.bulletinType = '" + type.getId() + "' AND "
					 + "bulletin.numberBulletin = '" + numberBulletin + "' AND "
					 + "bulletin.yearBulletin = '" + yearBulletin + "'";
		
		if(part != null){
			sql += " AND topic.part = '" + part + "'";
		}
		
		if(topic != null){
			sql += " AND topic = '" + topic.getId() + "'";
		}
		
		getData().put("sql", sql);
		return searchByHQL();
	}
	
}
